package com.site.action.backend.dining;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.site.dto.dining.RestaurantBizBean;
import com.site.dto.dining.RestaurantPhotoBizBean;
import com.site.service.dining.RestaurantPhotoService;
import com.site.util.base.action.BaseAction;
import com.site.util.base.service.BaseService;
import com.site.util.common.fileupload.FileUploadHandle;
import com.site.util.constant.Constants;

/**
 * 餐厅图片
 * @author win7
 *
 */
public class RestaurantPhotoAction extends BaseAction {
	private static final long serialVersionUID = -7789811238664723905L;

	private RestaurantPhotoService restaurantPhotoService;
	
	private List<RestaurantPhotoBizBean> restaurantPhotoBizBeans;
	
	private Long id;
	// 餐厅id
	private Long rid;
	
	public List<RestaurantPhotoBizBean> getRestaurantPhotoBizBeans() {
		return restaurantPhotoBizBeans;
	}
	public void setRestaurantPhotoBizBeans(
			List<RestaurantPhotoBizBean> restaurantPhotoBizBeans) {
		this.restaurantPhotoBizBeans = restaurantPhotoBizBeans;
	}

	@Resource
	public void setRestaurantPhotoService(
			RestaurantPhotoService restaurantPhotoService) {
		this.restaurantPhotoService = restaurantPhotoService;
	}
	
	/**
	 * 根据餐厅id,获得餐厅的主图
	 * @return
	 */
	public String getRestaurantMainPhotoById(){
		if(null != rid){
			restaurantPhotoBizBeans = restaurantPhotoService.getRestaurantMainPhotoById(rid);
			return "getRestaurantMainPhotoById";
		}
		return null;
	}
	
	/**
	 * 增加主图
	 * @return
	 */
	public String restaurantMainPhotoAdd(){
		if(null != files && !files.isEmpty()){
			// 上传对象 start 
			FileUploadHandle fileUploadHandle = new FileUploadHandle();
			fileUploadHandle.setRequest(request);
			// 取得上传文件类型
			fileUploadHandle.setAllowedTypes(Constants.UPDATE_FILE_TYPES);
			// 取得上传文件大小
			fileUploadHandle.setMaxSize(Constants.FILE_SIZE);
			fileUploadHandle.setFiles(files);
			fileUploadHandle.setFilesName(getFilesFileName());
			Map<String, Object> map = new HashMap<String, Object>();
			RestaurantBizBean restaurantBizBean = new RestaurantBizBean();
			restaurantBizBean.setId(rid);
			map.put("restaurantBizBean", restaurantBizBean);
			map.put("fileUploadHandle", fileUploadHandle);
			restaurantPhotoService.execute(map, "MAINPHOTOADD");
			super.addFieldError("success", "图片新增成功！");
		} else {
			super.addFieldError("success", "您还没有选择图片,请选择！");
		}
		return getRestaurantMainPhotoById();
	}
	
	/**
	 * 删除主图
	 * @return
	 */
	public String restaurantMainPhotoDelete(){
		if(null != id){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", id);
			map.put("request", request);
			restaurantPhotoService.execute(map, BaseService.COMMAND_DELETE);
		}
		return getRestaurantMainPhotoById();
	}
	
	public Long getRid() {
		return rid;
	}
	public void setRid(Long rid) {
		this.rid = rid;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
}
